/**
 *
 *基础app的js控件
 * Created by smc on 2017/9/18.
 */
var E = {
    100: "保存",
    101: "取消",
    102: "上一步",
    103: "下一步",
    104: "success",
    105: "error",
    106: "warn",
    801: [["#FF0094", "#ffffff", "#cccccc"], ["#A500FF", "#ffffff", "#cccccc"], ["#00AEB5", "#ffffff", "#cccccc"], ["#8CBE29", "#ffffff", "#cccccc"], ["#A54910", "#ffffff", "#cccccc"],
        ["#F79608", "#ffffff", "#cccccc"], ["#189EE7", "#ffffff", "#cccccc"], ["#EF1000", "#ffffff", "#cccccc"], ["#399A39", "#ffffff", "#cccccc"], ["#00458C", "#ffffff", "#cccccc"],
        ["#5A55B5", "#ffffff", "#cccccc"], ["#DE4D31", "#ffffff", "#cccccc"]
    ],                //背景色，前景色，边框色
    802: [10, 15, 20, 25, 30, 50, 100]
};


$.App = {}

$.App.DEFAULTS = {
    classes: 'table table-hover',
    sortClass: undefined,
    locale: undefined,
    height: getHeight(),
    undefinedText: '-',
    sortName: undefined,
    sortOrder: 'asc',
    sortStable: false,
    striped: false,
    columns: [[]],
    data: [],
    totalField: 'total',
    dataField: 'rows',
    method: 'post',
    url: undefined,
    ajax: undefined,
    cache: true,
    contentType: 'application/json',
    dataType: 'json',
    ajaxOptions: {},
    queryParams: function (params) {
        return params;
    },
    queryParamsType: 'limit', // undefined
    responseHandler: function (res) {
        return res;
    },
    pagination: true,
    onlyInfoPagination: false,
    paginationLoop: true,
    sidePagination: 'server', // client or server
    totalRows: 0, // server side need to set
    pageNumber: 1,
    pageSize: 10,
    pageList: [10, 25, 50, 100],
    paginationHAlign: 'right', //right, left
    paginationVAlign: 'bottom', //bottom, top, both
    paginationDetailHAlign: 'left', //right, left
    paginationPreText: '&lsaquo;',
    paginationNextText: '&rsaquo;',
    search: true,
    searchOnEnterKey: false,
    strictSearch: false,
    searchAlign: 'right',
    selectItemName: 'btSelectItem',
    showHeader: true,
    showFooter: false,
    showColumns: false,
    showPaginationSwitch: false,
    showRefresh: false,
    showToggle: false,
    buttonsAlign: 'right',
    smartDisplay: true,
    escape: false,
    minimumCountColumns: 1,
    idField: undefined,
    uniqueId: undefined,
    cardView: false,
    detailView: false,
    detailFormatter: function (index, row) {
        return '';
    },
    trimOnSearch: true,
    clickToSelect: false,
    singleSelect: false,
    toolbar: undefined,
    toolbarAlign: 'left',
    checkboxHeader: true,
    sortable: true,
    silentSort: true,
    maintainSelected: false,
    searchTimeOut: 500,
    searchText: '',
    iconSize: undefined,
    buttonsClass: 'default',
    iconsPrefix: 'glyphicon', // glyphicon of fa (font awesome)
    icons: {
        paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
        paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
        refresh: 'glyphicon-refresh icon-refresh',
        toggle: 'glyphicon-list-alt icon-list-alt',
        columns: 'glyphicon-th icon-th',
        detailOpen: 'glyphicon-plus icon-plus',
        detailClose: 'glyphicon-minus icon-minus'
    },

    customSearch: $.noop,

    customSort: $.noop,

    rowStyle: function (row, index) {
        return {};
    },

    rowAttributes: function (row, index) {
        return {};
    },

    footerStyle: function (row, index) {
        return {};
    },

    onAll: function (name, args) {
        return false;
    },
    onClickCell: function (field, value, row, $element) {
        return false;
    },
    onDblClickCell: function (field, value, row, $element) {
        return false;
    },
    onClickRow: function (item, $element) {
        return false;
    },
    onDblClickRow: function (item, $element) {
        return false;
    },
    onSort: function (name, order) {
        return false;
    },
    onCheck: function (row) {
        return false;
    },
    onUncheck: function (row) {
        return false;
    },
    onCheckAll: function (rows) {
        return false;
    },
    onUncheckAll: function (rows) {
        return false;
    },
    onCheckSome: function (rows) {
        return false;
    },
    onUncheckSome: function (rows) {
        return false;
    },
    onLoadSuccess: function (data) {
        return false;
    },
    onLoadError: function (status) {
        return false;
    },
    onColumnSwitch: function (field, checked) {
        return false;
    },
    onPageChange: function (number, size) {
        return false;
    },
    onSearch: function (text) {
        return false;
    },
    onToggle: function (cardView) {
        return false;
    },
    onPreBody: function (data) {
        return false;
    },
    onPostBody: function () {
        return false;
    },
    onPostHeader: function () {
        return false;
    },
    onExpandRow: function (index, row, $detail) {
        return false;
    },
    onCollapseRow: function (index, row) {
        return false;
    },
    onRefreshOptions: function (options) {
        return false;
    },
    onRefresh: function (params) {
        return false;
    },
    onResetView: function () {
        return false;
    }
}


$.App.home = {

    /**
     * 分页加载数据
     * @param obj
     */
    loadPage: function (obj) {
        options = $.extend($.App.DEFAULTS, obj);
        var url = obj.url;
        var id = obj.id;
        if (!url) {
            alert("请输入请求的url")
            return;
        }
        $('#' + id).bootstrapTable(options)
    },

    topMessage: function (type, msg, opts) {
        var opt = $.extend({
            timeout: 2000,
            clickToClose: true, 	//点击后隐藏
            waitForMove: true,		//超时后等待鼠标或键盘移动后隐藏
            baseCls: "humane-flatty",
            addnCls: (!type || type == E[106] ? "humane-flatty-info" : ( type == E[104] ? "humane-flatty-success" : "humane-flatty-error") ),
            success: function () {
            }
        }, opts || {});
        var notify = humane.create(opt)
        notify.log(msg, opt, function () {
            opt.success();
        });
    },

    removeData: function (id) {

    },
    clearFormData: function (formId) {  //清空form下面的数据
        $("#" + formId).resetForm();
    },
    validateForm: function (formId) {  //表单验证
        var validator = $('#' + formId).validate({
            highlight: function (input) {
                $(input).parents('.form-line').addClass('error');
            },
            unhighlight: function (input) {
                $(input).parents('.form-line').removeClass('error');
            },
            errorPlacement: function (error, element) {
                $(element).parents('.form-group').append(error);
            }
        });
        return validator;
    },
    serializeForm: function (formId) {
        return $("#" + formId).serializeJSON()
    },
    reloadTable: function (tableId) {
        $("#" + tableId).bootstrapTable("refresh");
    },
    showConfirmMessage: function (text, fun,value) {
        swal({
            title: "Are you sure?",
            text: text,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: '取消',
            closeOnConfirm: false
        }, function (bool) {
            if(bool){
                fun(value);
            }
        });
    },
    showSuccessMessage:function(){
        swal("Good job!", "很好操作成功!", "success");
    },
    showErrorMessage:function(){
        swal("SORRY!", "操作失败,请稍后重试!", "error");
    }
}


function getHeight() {
    return $(window).height() - $('.legel_footer').outerHeight() - $(".navbar").outerHeight() - 45;
}





